
require("movieclip")
local  ui = require("ui")
local widget = require("widget")
require("physics")

physics.start()
display.setStatusBar(display.HiddenStatusBar)

-- ==========================================================================================

local mainScreen = display.newGroup()
local dynamicScreen = display.newGroup()
local coverMenu = display.newGroup()

local coverBackground = display.newImageRect("CoverBackground.png", 320, 480)
coverBackground.x = 160
coverBackground.y = 240

local background = display.newImageRect("Background.png", display.contentWidth, display.contentHeight )
background.x = 160;
background.y = 240;

local city1 = display.newImageRect( "City1.png", 320, 325)
city1.x = 160
city1.y = 317

local city2 = display.newImageRect("City1.png", 320, 325)
city2.x = 480
city2.y = 351

local city3 = display.newImageRect("City2.png", 320,258)
city3.x = 160
city3.y = 351

local city4 = display.newImageRect("City2.png", 320,258)
city4.x = 480
city4.y = 351

local road1 = display.newImageRect( "Road.png", 320, 52)
road1.x = 160
road1.y = 500
road1.name = "road1"

local road2 = display.newImageRect( "Road.png", 320, 52)
road2.x = 480
road2.y = 500
road2.name = "road2"

local wall1 = display.newImageRect( "wall.png", 20, 100)
-- local wall1 = display.newImageRect( "wall.png", 20, 100)
-- local wall2 = display.newImageRect( "wall.png", 20, 100)
-- local roof1 = display.newImageRect( "roof.png", 20, 100) 

local car = movieclip.newAnim({"MyCar1.png", "MyCar2.png", "MyCar3.png", "MyCar4.png", "MyCar5.png", 
	                           "MyCar6.png", "MyCar7.png", "MyCar8.png", "MyCar9.png", "MyCar10.png"})
car:setSpeed(.8)
car.x = 120
car.y = 429	
car.width = 60
car.height = 60
car.name = "car"

local speed = 8
local totalLen = 0
local continueRun = false;
-- local city2 = display.newImageRect("City2.png", 320,258)
-- city2.x = 160
-- city2.y = 351

local ctlGoBtnOnEvent
local runCarFunc
local ctlGoBtn
 
local onStartTouch
local startBtnScaleAnim
local startBtn;

function resetWallRoofFunc()
    wall1:removeSelf( )
    -- wall2:removeSelf( )
    -- roof1:removeSelf( )

    wall1.x = 300
    wall1.y = 435
    wall1.rotation = 45
    wall1.name = "wall1"

    
    -- wall2.x = 400
    -- wall2.y = 425
    -- wall2.rotation = 0

    
    -- roof1.x = 350
    -- roof1.y = 365
    -- roof1.rotation = 90

    dynamicScreen:insert(wall1)
    -- dynamicScreen:insert(wall2)
    -- dynamicScreen:insert(roof1)

    physics.addBody(wall1, "static", {density = 4.0, fraction = 0.2, bounce = 0.2})
    -- physics.addBody(wall2, "dynamic", {density = 4.0, fraction = 0.2, bounce = 0.2})
    -- physics.addBody(roof1, "dynamic", {density = 4.0, fraction = 0.2, bounce = 0.2})
end



local resetWallRoofFuncAtFixLenFunc = function()
    if totalLen % 1040 == 0 then
	    resetWallRoofFunc()
    end
end

local runCarFunc = function()
    totalLen = totalLen + speed
	for i = 1, dynamicScreen.numChildren, 1 do		
		dynamicScreen[i].x = dynamicScreen[i].x - speed		

	    if dynamicScreen[i].x == -160 then
		    dynamicScreen[i].x = 480
	    end
	end
end

local adjustCarPosFunc = function()
    if continueRun == true then 
    	local vx, vy = car:getLinearVelocity( );
    	car:setLinearVelocity( 0, vy)
    	car.x = car.x + 2
    end

    --Keep the car at fixed position-- 
	if car.x >= (display.contentWidth / 2) then
		car.x = (display.contentWidth / 2)
	elseif car.x <= 60 then
		car.x = 60
	end
end

local carCollicionHandleFunc = function(event)   
    if event.object2 ~= nil and event.object2.name ~= nil and event.object2.name == "car" then
    	print( "object2 = ".. event.object2.name)

    	if event.object1 ~= nil and event.object1.name ~= nil then
    	    print( "object1 = ".. event.object1.name..", rotation = "..event.object1.rotation)

            timer.performWithDelay( 10, function()
                local vx, vy = car:getLinearVelocity( );
    	        car:setLinearVelocity( vx, 0)        	
            	car.rotation = event.object1.rotation * -1
            end, 1)    	    
        end
    end
end	

-- car:addEventListener( "collision", carCollicionHandleFunc)


local ctlGoBtnOnEvent = function(event)
	if event.phase == "began" then
    	continueRun = true
    	car:play()    	
    elseif event.phase == "ended" then
        continueRun = false
        car:stopAtFrame(1)
    end	   
end

local ctlGoBtn = widget.newButton{
	defaultFile = "button.png",	
	overSrc = "buttonOver.png",	
	label = "Go",
	font = native.systemFontBold,
	labelColor = {default = {0, 0, 0}},
	emboss = true,
	onEvent = ctlGoBtnOnEvent
}
ctlGoBtn.y = 420
ctlGoBtn.x = 275 
ctlGoBtn.xScale = 0.7
ctlGoBtn.yScale = 0.7  

-- local startBtn = ui.newButton{
-- 	defaultSrc = "CoverButton.png",
-- 	defaultX = 232,
--     defaultY = 68,
--     overSrc = "CoverButtonPressed.png",
--     overX = 232,
--     overY = 68,
--     onEvent = onStartTouch
-- }

           

-- local car = display.newImageRect( "Car.png", 129,99)
-- car.x = 83
-- car.y = 379

--=======================================================================================

-- local timeLabel = display.newText("", 160, 10, native.SystemFontBold, 20, "center")
-- timer.performWithDelay( 1000, function()
-- 	timeLabel.text = os.date()
-- end, 0)
-- timeLabel:setTextColor( 0, 0, 0 )


-- local printWhenItsOver = function()
-- 	print("transition complete")
-- end

-- transition.to( car, {time = 2400, x =250, y = 100, rotation = 720, transition = easing.outExpo, onComplete = printWhenItsOver})
-- timer.performWithDelay(2000,  function () 
-- 	transition.to( car, {time = 1200, x = 83, y = 379, rotation = -720, transition = easing.inExpo})
-- end)


-- local justTouchScreen = function (event)
--     if event.phase == "ended" then
--         if event.xStart < event.x and (event.x - event.xStart) >= 30 then
--         	car.x = car.x + 10
--         	return true
--         end

--         if event.xStart > event.x and (event.xStart - event.x) >= 30 then
--         	car.x = car.x - 10
--         	return true
--         end

--         if event.yStart < event.y and (event.y - event.yStart) >= 30 then
--         	car.y = car.y + 10
--         	return true
--         end

--         if event.yStart > event.y and (event.yStart - event.y) >= 30 then
--         	car.y = car.y - 10
--         	return true
--         end

--     end
-- end

-- Runtime:addEventListener( "touch", justTouchScreen)

-- Menu Cover --

onStartTouch = function(event) 
	if event.phase == "began" then	    
		transition.to( coverMenu, {time = 2000, alpha = 0, onComplete = function ()
		    coverMenu.isVisible = false	
		    		    
		    transition.to( mainScreen, {time = 500, alpha = 1, onComplete = function ()		   
		    mainScreen.isVisible = true
            dynamicScreen.isVisible = true            

		    end})	     
		end})
	end
end

startBtn = ui.newButton{
	defaultSrc = "CoverButton.png",
	defaultX = 232,
    defaultY = 68,
    overSrc = "CoverButtonPressed.png",
    overX = 232,
    overY = 68,
    onPress = onStartTouch
}
startBtn.x = 160
startBtn.y = 350

startBtnScaleAnim = function()
	transition.to( startBtn, {time = 800, xScale = 1.06, yScale = 1.06, onComplete = function()
		transition.to( startBtn, {time = 800, xScale = 1, yScale =1, onComplete = function()
			 startBtnScaleAnim()
		end} )
	end})
end

coverMenu:insert(coverBackground)
coverMenu:insert(startBtn)
--Start the start button bounce animation--
startBtnScaleAnim()

-- Main Screen --

mainScreen:insert(background)
mainScreen:insert(dynamicScreen)
mainScreen:insert(car)
mainScreen:insert(ctlGoBtn)
mainScreen.isVisible = false

dynamicScreen:insert(city1)
dynamicScreen:insert(city2)
dynamicScreen:insert(city3)
dynamicScreen:insert(city4)
dynamicScreen:insert(road1)
dynamicScreen:insert(road2)
dynamicScreen.isVisible = false

resetWallRoofFunc()

physics.addBody(car, "dynamic", {density = 15.0, fraction = 0.5, bounce = 0.1, radius = 30})


physics.addBody(road1, "static", {fraction = 0.3, bounce = 0.3})
physics.addBody(road2, "static", {fraction = 0.3, bounce = 0.3})


function frameUpdate(event)
	if continueRun == true then
		runCarFunc()
	end
end
Runtime:addEventListener("enterFrame", frameUpdate)
Runtime:addEventListener("enterFrame", adjustCarPosFunc)
Runtime:addEventListener("enterFrame", resetWallRoofFuncAtFixLenFunc)
Runtime:addEventListener("preCollision", carCollicionHandleFunc)
-- local speed = 2
-- city1.speed = speed
-- city2.speed = speed
-- city3.speed = speed
-- city4.speed = speed
-- road1.speed = speed
-- road2.speed = speed

-- function scrollMyCity(self, event)
-- 	self.x = self.x - self.speed
-- 	if self.x == -160 then
-- 		self.x = 480
-- 	end

-- end

-- city1.enterFrame = scrollMyCity
-- city2.enterFrame = scrollMyCity
-- city3.enterFrame = scrollMyCity
-- city4.enterFrame = scrollMyCity
-- road1.enterFrame = scrollMyCity
-- road2.enterFrame = scrollMyCity
-- Runtime:addEventListener("enterFrame", city1)
-- Runtime:addEventListener( "enterFrame", city2)
-- Runtime:addEventListener("enterFrame", city3)
-- Runtime:addEventListener( "enterFrame", city4)
-- Runtime:addEventListener( "enterFrame", road1)
-- Runtime:addEventListener( "enterFrame", road2)